package org.iscas.oilreservoir.service.impl;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.iscas.oilreservoir.mapper.UserMapper;
import org.iscas.oilreservoir.po.User;
import org.iscas.oilreservoir.po.UserCustom;
import org.iscas.oilreservoir.po.UserQueryVo;
import org.junit.Before;

public class UserServiceImpl implements UserMapper {
	private SqlSessionFactory sqlSessionFactory;

	// 此方法是在执行testFindUserById之前执行
	@Before
	public void setUp() throws Exception {
		// 创建sqlSessionFactory

		// mybatis配置文件
		String resource = "SqlMapConfig.xml";
		// 得到配置文件流
		InputStream inputStream = Resources.getResourceAsStream(resource);

		// 创建会话工厂，传入mybatis的配置文件信息
		sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
	}

	public void deleteUser(int id) throws Exception {
		// TODO Auto-generated method stub

	}

	public User findUserById(int id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public User findUserByIdResultMap(int id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public List<User> findUserByName(String name) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public int findUserCount(UserQueryVo userQueryVo) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	public List<UserCustom> findUserList(UserQueryVo userQueryVo)
			throws Exception {
		SqlSession sqlSession = sqlSessionFactory.openSession();

		// 创建UserMapper对象，mybatis自动生成mapper代理对象
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

		// 创建包装对象，设置查询条件
		UserCustom userCustom = new UserCustom();
		// 由于这里使用动态sql，如果不设置某个值，条件不会拼接在sql中
		// userCustom.setSex("1");
		userCustom.setUsername("小明");
		// 传入多个id
		List<Integer> ids = new ArrayList<Integer>();
		ids.add(1);
		ids.add(10);
		ids.add(16);
		// 将ids通过userQueryVo传入statement中
		userQueryVo.setIds(ids);
		userQueryVo.setUserCustom(userCustom);
		// 调用userMapper的方法

		List<UserCustom> list = userMapper.findUserList(userQueryVo);

		return list;
	}

	public void insertUser(User user) throws Exception {
		// TODO Auto-generated method stub

	}

}
